package com.agency.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Hashtable;

/**
 * <ul>
 * 代理商或商户邀请注册
 * </ul>
 * Created by mclubing on 16-6-13.
 *
 * @author mclubing
 */
@Controller
public class InviteToRegistController {

    private static Logger LOGGER = Logger.getLogger(InviteToRegistController.class);

    @RequestMapping(value = "invite.htm")
    public void invite(Model model, HttpServletRequest request, HttpServletResponse response, @RequestParam("agentId") String agentId) {
        String url = "http://localhost:8080/merchant/invitePage.htm?agentId=" + agentId;
        String format = "png";
        int height = 200;
        int width = 200;
        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
            MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (Exception e) {
            LOGGER.error("QRCode generates error!", e);
            e.printStackTrace();
        }
    }
}
